package com.yanqu.road.dao.impl.activity.hsisland;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandBattleFieldDamageData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HsIslandBattleFieldDamageDataDaoImpl extends TempDataObjectDao<HsIslandBattleFieldDamageData> {

    public static String getHsIslandBattleFieldDamageDataMapSql = "select * from `t_c_activity_hs_island_battle_field_damage_data` where `activityId`=? and `groupId`=? and battleFieldId=? and monsterId=?";

    public Map<Long, HsIslandBattleFieldDamageData> getHsIslandDamageDataMap(int activityId, int groupId, long battleFieldId, int monsterId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            Map<Long, HsIslandBattleFieldDamageData> map = new ConcurrentHashMap<>();
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getHsIslandBattleFieldDamageDataMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            call.setLong(i++, battleFieldId);
            call.setInt(i++, monsterId);
            rs = call.executeQuery();
            while (rs.next()) {
                HsIslandBattleFieldDamageData data = parse(rs);
                map.put(data.getUserId(), data);
            }
            return map;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
    }

    public static String getAllBattleFieldByMonsterIdSql = "SELECT DISTINCT `battleFieldId` FROM `t_c_activity_hs_island_battle_field_damage_data` WHERE `activityId`=? and `groupId`=? and `monsterId` = ?";

    public List<Long> getAllBattleFieldByMonsterId(int activityId, int groupId, int monsterId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<Long> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getAllBattleFieldByMonsterIdSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            call.setInt(i++, monsterId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(rs.getLong("battleFieldId"));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateHsIslandUnionDamageDataSql = "update `t_c_activity_hs_island_battle_field_damage_data` set totalDamage=?,times=?,updateTime=? where activityId=? and groupId=? and unionId=? and userId = ? and battleFieldId=? and monsterId = ?";
    @Override
    protected boolean updateData(HsIslandBattleFieldDamageData hsIslandBattleFieldDamageData) {
        hsIslandBattleFieldDamageData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandBattleFieldDamageData.getTotalDamage()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandBattleFieldDamageData.getTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandBattleFieldDamageData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandBattleFieldDamageData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandBattleFieldDamageData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandBattleFieldDamageData.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandBattleFieldDamageData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandBattleFieldDamageData.getBattleFieldId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandBattleFieldDamageData.getMonsterId()));
        if (getDb().executeNoneQuery(updateHsIslandUnionDamageDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHsIslandUnionDamageDataSql = "insert into `t_c_activity_hs_island_battle_field_damage_data`(activityId,groupId,unionId,userId,battleFieldId,monsterId,totalDamage,times,updateTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(HsIslandBattleFieldDamageData hsIslandBattleFieldDamageData) {
        hsIslandBattleFieldDamageData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandBattleFieldDamageData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandBattleFieldDamageData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hsIslandBattleFieldDamageData.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandBattleFieldDamageData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandBattleFieldDamageData.getBattleFieldId()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandBattleFieldDamageData.getMonsterId()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandBattleFieldDamageData.getTotalDamage()));
        para.put(i++, new DbParameter(Types.INTEGER, hsIslandBattleFieldDamageData.getTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, hsIslandBattleFieldDamageData.getUpdateTime()));
        if (getDb().executeNoneQuery(insertHsIslandUnionDamageDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HsIslandBattleFieldDamageData parseData(ResultSet rs) throws SQLException {
        HsIslandBattleFieldDamageData hsIslandBattleFieldDamageData = new HsIslandBattleFieldDamageData();
        hsIslandBattleFieldDamageData.setActivityId(rs.getInt("activityId"));
        hsIslandBattleFieldDamageData.setGroupId(rs.getInt("groupId"));
        hsIslandBattleFieldDamageData.setUnionId(rs.getString("unionId"));
        hsIslandBattleFieldDamageData.setUserId(rs.getLong("userId"));
        hsIslandBattleFieldDamageData.setBattleFieldId(rs.getLong("battleFieldId"));
        hsIslandBattleFieldDamageData.setMonsterId(rs.getInt("monsterId"));
        hsIslandBattleFieldDamageData.setTotalDamage(rs.getLong("totalDamage"));
        hsIslandBattleFieldDamageData.setTimes(rs.getInt("times"));
        hsIslandBattleFieldDamageData.setUpdateTime(rs.getLong("updateTime"));
        return hsIslandBattleFieldDamageData;
    }
}
